package exercise17;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Exercise17_3 extends JFrame{
	private SinglePanel spanel = new SinglePanel();
	private JRadioButton jrbRed = new JRadioButton("Red");
	private JRadioButton jrbYellow = new JRadioButton("Yellow");
	private JRadioButton jrbGreen = new JRadioButton("Green");
	
	public Exercise17_3(){
		
		setLayout(new BorderLayout(5, 5));
		Border lineborder = new LineBorder(Color.BLACK, 1);
		
		JPanel optionPanel = new JPanel(new FlowLayout());
		jrbRed.setMnemonic('R');
		jrbYellow.setMnemonic('Y');
		jrbGreen.setMnemonic('G');
		
		optionPanel.add(jrbRed);
		optionPanel.add(jrbYellow);
		optionPanel.add(jrbGreen);
		
		ButtonGroup group = new ButtonGroup();
		group.add(jrbRed);
		group.add(jrbYellow);
		group.add(jrbGreen);
		
		spanel.setBorder(lineborder);
		optionPanel.setBorder(lineborder);
		add(spanel, BorderLayout.CENTER);
		add(optionPanel, BorderLayout.SOUTH);
		
		jrbRed.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				spanel.setColor(SinglePanel.RED);
			}
		});
		
		jrbYellow.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				spanel.setColor(SinglePanel.YELLOW);
			}
		});
		
		jrbGreen.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				spanel.setColor(SinglePanel.GREEN);
			}
		});
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Exercise17_3 frame = new Exercise17_3();
		frame.setTitle("Exercise17_3");
//		frame.pack();
		frame.setSize(300,300);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}
	
	class SinglePanel extends JPanel{
		private final static int RED = 0;
		private final static int YELLOW = 1;
		private final static int GREEN = 2;
		private int color = 3;
		
		public void setColor(int color){
			this.color = color;
			repaint();
		}
		
		protected void paintComponent(Graphics g){
			super.paintComponent(g);
			
			int xCenter = getWidth() / 2;
			int lampWidth = getWidth() / 5;
			int lampRadio = (int) (lampWidth / 2 * 0.8);
			int interval = (getHeight() - 2 * lampRadio * 3) / 6;
			
			g.drawRect(xCenter - lampWidth / 2, getHeight() - 2 * lampRadio * 3 - 5 * interval, lampWidth, 2 * lampRadio * 3 + 4 * interval);
			//中间的黄灯
			g.setColor(Color.YELLOW);
			g.drawOval(xCenter - lampRadio, getHeight() / 2 - lampRadio, 2 * lampRadio, 2 * lampRadio);
			if (color == YELLOW) {
				g.fillOval(xCenter - lampRadio, getHeight() / 2 - lampRadio, 2 * lampRadio, 2 * lampRadio);
			}
			//上面的红灯
			g.setColor(Color.RED);
			g.drawOval(xCenter - lampRadio, getHeight() / 2 - lampRadio - 2 * lampRadio - interval, 2 * lampRadio, 2 * lampRadio);
			if (color == RED) {
				g.fillOval(xCenter - lampRadio, getHeight() / 2 - lampRadio - 2 * lampRadio - interval, 2 * lampRadio, 2 * lampRadio);
			}
			//下面的绿灯
			g.setColor(Color.GREEN);
			g.drawOval(xCenter - lampRadio, getHeight() / 2 - lampRadio + 2 * lampRadio + interval, 2 * lampRadio, 2 * lampRadio);
			if (color == GREEN) {
				g.fillOval(xCenter - lampRadio, getHeight() / 2 - lampRadio + 2 * lampRadio + interval, 2 * lampRadio, 2 * lampRadio);
			}
			
			
		}
		
		public Dimension getPerferredSize() {
			return new Dimension(300, 300);
		}
	}

}
